package com.star.warmwinter.module.user.viewCtrl;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.star.tool.util.RegexUtils;
import com.star.warmwinter.R;
import com.star.warmwinter.databinding.CommonLoginCodeBinding;
import com.star.warmwinter.module.user.ui.CommonLoginCodeActivity;
import com.star.warmwinter.module.user.ui.CommonLoginPwdActivity;
import com.star.warmwinter.module.user.ui.CommonLoginTelActivity;
import com.star.tool.util.FuncUtil;
import com.star.warmwinter.widget.ThemeToast;

/**
 * Author: blackbai11
 * Date: 2018/5/31
 * Description: 忘记密码页面控制器({@link CommonLoginTelActivity})
 */
public class ForgetPwdCtrl extends CommonLoginCtrl implements View.OnClickListener {

    private CommonLoginCodeBinding mBinding;
    private CountDownTimer mTimer;

    public ForgetPwdCtrl(Context context) {
        super(context);
    }

    public ForgetPwdCtrl(Context context, CommonLoginCodeBinding binding) {
        super(context);
        mBinding = binding;
        mTimer = FuncUtil.VerificationCodeTimer(mBinding.tvGetCode);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tel_country:
                Toast.makeText(mContext, "国家", Toast.LENGTH_SHORT).show();
                break;

            case R.id.img_next:
                if (TextUtils.isEmpty(commonLoginVM.getPhone())) {
                    ThemeToast.showShort("手机号不能为空");
                    return;
                }
                if (!RegexUtils.isMobileExact(commonLoginVM.getPhone())) {
                    ThemeToast.showShort("请输入正确的手机号");
                    return;
                }
                Intent intent = new Intent(mContext, CommonLoginCodeActivity.class);
                intent.putExtra(CommonLoginTelActivity.LOGIN_TYPE_KEY, CommonLoginTelActivity.LOGIN_TYPE_FORGET_PWD);
                mContext.startActivity(intent);
                break;

            case R.id.tv_get_code:
                ThemeToast.showShort("验证码已发送");
                mTimer.start();
                break;

            case R.id.img_code_next:
                if (TextUtils.isEmpty(commonLoginVM.getCode())) {
                    ThemeToast.showShort("请先输入验证码");
                    return;
                }
                if (commonLoginVM.getCode().length() != 6) {
                    ThemeToast.showShort("验证码为6位长度");
                    return;
                }

                //网络请求-检测验证码
                //验证码错误 -提示
                //验证码正确 -跳转设置密码
                intent = new Intent(mContext, CommonLoginPwdActivity.class);
                intent.putExtra(CommonLoginTelActivity.LOGIN_TYPE_KEY, CommonLoginTelActivity.LOGIN_TYPE_FORGET_PWD);
                mContext.startActivity(intent);
                break;

            case R.id.btn_login:
                if (TextUtils.isEmpty(commonLoginVM.getPwd())) {
                    ThemeToast.showShort("密码不能为空");
                    return;
                }
                if (6 > commonLoginVM.getPwd().length() || 16 < commonLoginVM.getPwd().length()) {
                    ThemeToast.showShort("密码必须为6到16位");
                    return;
                }
                if (TextUtils.isEmpty(commonLoginVM.getPwd2())) {
                    ThemeToast.showShort("请再次输入密码");
                    return;
                }
                if (!commonLoginVM.getPwd2().equals(commonLoginVM.getPwd())) {
                    ThemeToast.showShort("两次密码不一致");
                    return;
                }
                //网络请求-修改密码
                //修改成功
                ThemeToast.showShort("修改成功");
                break;
        }
    }

}
